package com.cloudbroker.bcs.csvc.lightinvesting.facade.service;

import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.service.LedSecService;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.baseRelevanted.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.cashTransactions.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.interestRelated.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.*;
import com.cloudbroker.bcs.csvc.lightinvesting.facade.service.aspect.UserCache;
import com.cloudbroker.bcs.csvc.lightinvesting.facade.service.aspect.UserTokenUtil;
import org.springframework.stereotype.Service;
import util.RSAEncrypt;

import javax.annotation.Resource;

import java.util.Collections;

import static com.cloudbroker.bcs.csvc.lightinvesting.api.constants.LedT2FunctionIds.*;

/**
 * Created by fangyf18371 on 2016/8/19.
 */
@Service
public class LedSecServiceFacade implements LedSecService{
    @Resource
    private LedT2RemoteService ledT2RemoteService;

    @Override
    public WithdrawEntrusQryBackVo func_comentrust_detail_withdraw_qry(WithdrawEntrusForm param) {
        return ledT2RemoteService.callT2(COMENTRUST_DETAIL_WITHDRAW_QRY,param,WithdrawEntrusQryBackVo.class);
    }

    @Override
    public StocksEntrustWithdrawVo func_comentrust_detail_order_withdraw(StocksEntrustWithdrawForm param) {
        return ledT2RemoteService.callT2(COMENTRUST_DETAIL_ORDER_WITHDRAW,param,StocksEntrustWithdrawVo.class);
    }

    @Override
    public BankfundQryBackVo func_bankfund_qry(BankfundQryForm param) {
        return ledT2RemoteService.callT2(BANKFUND_QRY,param,BankfundQryBackVo.class);
    }

    @Override
    public BktransferQryBackVo func_bktransfer_qry(BktransferQryForm param) {
        return ledT2RemoteService.callT2(BKTRANSFER_QRY,param,BktransferQryBackVo.class);
    }

    @Override
    public BanktransferVo func_banktransfer(BanktransferForm param) {
        String transfer_direction=param.getTransfer_direction();
        if(transfer_direction.equals("1")){//银行转证券传银行密码
             String bank_password=param.getBank_password();
            byte[] res=RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(), util.Base64.decode(bank_password));
            param.setBank_password(new String(res));
        }else if(transfer_direction.equals("2")){//证券转银行传资金密码
            String fund_password=param.getFund_password();
            byte[] res=RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(), util.Base64.decode(fund_password));
            param.setFund_password(new String(res));
        }

//        byte[] res=RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(), util.Base64.decode(fund_password));
//        byte[] reb=RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(), util.Base64.decode(bank_password));
//        param.setFund_password(new String(res));
//        param.setBank_password(new String(reb));
        return ledT2RemoteService.callT2(BANKTRANSFER,param,BanktransferVo.class);
    }

    @Override
    public BankbalanceQryVo func_bankbalance_qry(BankbalanceQryForm param) {
        return ledT2RemoteService.callT2(BANKBALANCE_QRY,param,BankbalanceQryVo.class);
    }

    @Override
    public RealtimeQryBackVo func_realtime_qry(RealtimeQryForm param) {
        return ledT2RemoteService.callT2(REALTIME_QRY,param,RealtimeQryBackVo.class);
    }

    @Override
    public BankaccoutQryBackVo func_bankaccout_qry(BankaccoutQryForm param) {
        return ledT2RemoteService.callT2(BANKACCOUT_QRY,param,BankaccoutQryBackVo.class);
    }

    @Override
    public HisRealtimeQryBackVo func_his_realtime_qry(HisRealtimeQryForm param) {
        return ledT2RemoteService.callT2(HIS_REALTIME_QRY,param,HisRealtimeQryBackVo.class);
    }

    @Override
    public AlmostbuyQryVo func_almostbuy_qry(AlmostbuyQryForm param) {
        return ledT2RemoteService.callT2(ALMOSTBUY_QRY,param,AlmostbuyQryVo.class);
    }

    @Override
    public SecuritiesDealQryBackVo func_securities_deal_qry(SecuritiesDealQryForm param) {
        return ledT2RemoteService.callT2(SECURITIES_DEAL_QRY,param,SecuritiesDealQryBackVo.class);
    }

    @Override
    public IncomeBalanceQryVo func_income_balance_qry(IncomeBalanceQryForm param) {
        return ledT2RemoteService.callT2(INCOME_BALANCE_QRY,param,IncomeBalanceQryVo.class);
    }

    @Override
    public HisComFundstockQryBackVo func_his_com_fundstock_qry(HisComFundstockQryForm param) {
        return ledT2RemoteService.callT2(HIS_COM_FUNDSTOCK_QRY,param,HisComFundstockQryBackVo.class);
    }

    @Override
    public HisComDeliverQryBackVo func_his_com_deliver_qry(HisComDeliverQryForm param) {
        return ledT2RemoteService.callT2(HIS_COM_DELIVER_QRY,param,HisComDeliverQryBackVo.class);
    }

    @Override
    public HisComSecuipoinfoQryBackVo func_his_com_secuipoinfo_qry(HisComSecuipoinfoQryForm param) {
        return ledT2RemoteService.callT2(HIS_COM_SECUIPOINFO_QRY,param,HisComSecuipoinfoQryBackVo.class);
    }

    @Override
    public HisComMatchinfoNopwdQryBackVo func_his_com_matchinfo_nopwdqry(HisComMatchinfoNopwdQryForm param) {
        return ledT2RemoteService.callT2(HIS_COM_MATCHINFO_NOPWDQRY,param,HisComMatchinfoNopwdQryBackVo.class);
    }

    @Override
    public HisComLuckyinfonNopwdQryBackVo func_his_com_luckyinfo_nopwdqry(HisComLuckyinfonNopwdQryForm param) {
        return ledT2RemoteService.callT2(HIS_COM_LUCKYINFO_NOPWDQRY,param,HisComLuckyinfonNopwdQryBackVo.class);
    }

    @Override
    public TradPwdModVo func_trad_pwd_mod(TradPwdModForm param) {
        String new_password=param.getNew_password();
        //new_password解密
        byte[] res= RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(), util.Base64.decode(new_password));
        param.setNew_password(new String(res));
//        TradPwdModVo tradPwdModVo=ledT2RemoteService.callT2(TRAD_PWD_MOD,param,TradPwdModVo.class);
//        if("0".equals(tradPwdModVo.getError_no())){
//            BrokerVO brokerVO = new BrokerVO();
//            UserTokenUtil.setBGBrokerVO(brokerVO, param);
//            UserCache userCache = new UserCache();
//            userCache.setBrokerVO(brokerVO);
////            userCache.setAccount_content(param.getNew_password());
//            String user_token = UserTokenUtil.addUserToken(userCache);
////            tradPwdModVo.setUser_token(user_token);
//        }
        return ledT2RemoteService.callT2(TRAD_PWD_MOD,param,TradPwdModVo.class);
//        return tradPwdModVo;
    }

    @Override
    public RealtimeCombineQryBackVo func_realtime_combine_qry(RealtimeCombineQryForm param) {
        return ledT2RemoteService.callT2(REALTIME_COMBINE_QRY,param,RealtimeCombineQryBackVo.class);
    }

    @Override
    public CombineAdjustQryBackVo func_combine_adjust_qry(CombineAdjustQryForm param) {
        return ledT2RemoteService.callT2(COMBINE_ADJUST_QRY,param,CombineAdjustQryBackVo.class);
    }

    @Override
    public UserTransferQryBackVo func_user_transfer_qry(UserTransferQryForm param) {
//        if("2033".equals(param.getCompany_id())) {
//            UserCache userCache = util.UserTokenUtil.queryUserToken(param.getUser_token());
//            if ("0".equals(userCache.getIsCapitalAccount())) {
//                UserTransferQryBackVo userTransferQryBackVo = new UserTransferQryBackVo();
//                userTransferQryBackVo.setData_list(Collections.EMPTY_LIST);
//                return userTransferQryBackVo;
//            }
//        }
        return ledT2RemoteService.callT2(USER_TRANSFER_QRY,param,UserTransferQryBackVo.class);
    }

    @Override
    public CompayApplyVo func_compay_apply(CompayApplyForm param) {
        String passwordTypte=param.getPassword_type();
        if(passwordTypte.equals("2")) {//如果为交易密码
            String password=param.getPassword();
            //new_password解密
            byte[] res = RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(), util.Base64.decode(password));
            param.setPassword(new String(res));
        }
        return ledT2RemoteService.callT2(COMPAY_APPLY,param,CompayApplyVo.class);
    }

    @Override
    public CompayIdentifyVo func_compay_identify(CompayIdentifyForm param) {
        return ledT2RemoteService.callT2(COMPAY_IDENTIFY,param,CompayIdentifyVo.class);
    }

    @Override
    public CompayConfirmVo func_compay_confirm(CompayConfirmForm param) {
        return ledT2RemoteService.callT2(COMPAY_CONFIRM,param,CompayConfirmVo.class);
    }

    @Override
    public CompayApplyQryBackVo func_compay_apply_qry(CompayApplyQryForm param) {
        return ledT2RemoteService.callT2(COMPAY_APPLY_QRY,param,CompayApplyQryBackVo.class);
    }

    @Override
    public ComuserBusinacctQryBackVo func_comuser_businacct_qry(ComuserBusinacctQryForm param) {
        return ledT2RemoteService.callT2(COMUSER_BUSINACCT_QRY,param,ComuserBusinacctQryBackVo.class);
    }

    @Override
    public CombineSearchFormQryBackVo func_combine_search(CombineSearchForm param) {
        return ledT2RemoteService.callT2(COMBINE_SEARCH,param,CombineSearchFormQryBackVo.class);
    }

    @Override
    public ComadviserBuildVo func_comadviser_build(ComadviserBuildForm param) {
        return ledT2RemoteService.callT2(COMADVISER_BUILD,param,ComadviserBuildVo.class);
    }

    @Override
    public CombineFocusRenewVo func_combine_focus_renew(CombineFocusRenewForm param) {
        return ledT2RemoteService.callT2(COMBINE_FOCUS_RENEW,param,CombineFocusRenewVo.class);
    }

    @Override
    public ComuserSignRenewVo func_comuser_sign_renew(ComuserSignRenewForm param) {
        return ledT2RemoteService.callT2(COMUSER_SIGN_RENEW,param,ComuserSignRenewVo.class);
    }

    @Override
    public CombineFocusNumQryBackVo func_combine_focus_num_qry(CombineFocusNumQryForm param) {
        return ledT2RemoteService.callT2(COMBINE_FOCUS_NUM_QRY,param,CombineFocusNumQryBackVo.class);
    }

    @Override
    public CombineCollectNumQryBackVo func_combine_collect_num_qry(CombineCollectNumQryForm param) {
        return ledT2RemoteService.callT2(COMBINE_COLLECT_NUM_QRY,param,CombineCollectNumQryBackVo.class);
    }

    @Override
    public ComadviserteamQryBackVo func_comadviserteam_qry(ComadviserteamQryForm param) {
        return ledT2RemoteService.callT2(COMADVISERTEAM_QRY,param,ComadviserteamQryBackVo.class);
    }

    @Override
    public TipInfoQryBackVo func_tip_info_qry(TipInfoQryForm param) {
        return ledT2RemoteService.callT2(TIP_INFO_QRY,param,TipInfoQryBackVo.class);
    }

    @Override
    public ComadviserBginfoModVo func_comadviser_bginfo_mod(ComadviserBginfoModForm param) {
        //增加投顾简介修改频率校验 false -- 不允许修改
        if (!UserTokenUtil.leaderBackgroundEditVerify(param.getUser_id(), param.getCompany_id())){
            return VOUtil.genErrorResult("30001","投顾简介修改太频繁，请稍后再试", ComadviserBginfoModVo.class);
        }
        param.setAdviser_id(param.getUser_id());
        return ledT2RemoteService.callT2(COMADVISER_BGINFO_MOD,param,ComadviserBginfoModVo.class);
    }

    @Override
    public ExtcombineCombinestatusModVo func_extcombine_combinestatus_mod(ExtcombineCombinestatusModForm param) {
        return ledT2RemoteService.callT2(EXTCOMBINE_COMBINESTATUS_MOD,param,ExtcombineCombinestatusModVo.class);
    }

    @Override
    public ComuserRelationNumQryVo func_comuser_relation_num_qry(ComuserRelationNumQryForm param) {
        return ledT2RemoteService.callT2(COMUSER_RELATION_NUM_QRY,param,ComuserRelationNumQryVo.class);
    }

    @Override
    public AdviserFocusNumQryVo func_adviser_focus_num_qry(AdviserFocusNumQryForm param) {
        return ledT2RemoteService.callT2(ADVISER_FOCUS_NUM_QRY,param,AdviserFocusNumQryVo.class);
    }

    @Override
    public AdviserCollectNumQryBackVo func_adviser_collect_num_qry(AdviserCollectNumQryForm param) {
        return ledT2RemoteService.callT2(ADVISER_COLLECT_NUM_QRY,param,AdviserCollectNumQryBackVo.class);
    }

    @Override
    public ComdataAdviserbineSuccessrateQryBackVo func_comdata_adviserbine_successrate_qry(ComdataAdviserbineSuccessrateQryForm param) {
        return ledT2RemoteService.callT2(COMDATA_ADVISERBINE_SUCCESSRATE_QRY,param,ComdataAdviserbineSuccessrateQryBackVo.class);
    }

    @Override
    public CombineapplyQueryBackVo func_combineapply_query(CombineapplyQueryForm param) {
        return ledT2RemoteService.callT2(COMBINEAPPLY_QUERY,param,CombineapplyQueryBackVo.class);
    }

    @Override
    public ComtradeEntrustreasonModVo func_comtrade_entrustreason_mod(ComtradeEntrustreasonModForm param) {
        return ledT2RemoteService.callT2(COMTRADE_ENTRUSTREASON_MOD,param,ComtradeEntrustreasonModVo.class);
    }

    @Override
    public ExtcomacctVagueQryBackVo func_extcomacct_vague_qry(ExtcomacctVagueQryForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_VAGUE_QRY,param,ExtcomacctVagueQryBackVo.class);
    }

    @Override
    public UnsubscribeRefundVo func_extcombine_refundbalance_qry(UnsubscribeRefundForm param) {
        return ledT2RemoteService.callT2(EXTCOMBINE_REFUNDBALANCE_QRY,param,UnsubscribeRefundVo.class);
    }

    @Override
    public ExtcomaRfundqryVo func_extcomacct_refundbalance_qry(ExtcomaRfundqryForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_REFUNDBALANCE_QRY,param,ExtcomaRfundqryVo.class);
    }

    @Override
    public ExtcomacctAdviserRecommendVo func_extcomacct_adviser_recommend(ExtcomacctAdviserRecommendForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_ADVISER_RECOMMEND,param,ExtcomacctAdviserRecommendVo.class);
    }

    @Override
    public ExtcomacctAdviserRecommendCancelVo func_extcomacct_adviser_recommend_cancel(ExtcomacctAdviserRecommendCancelForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_ADVISER_RECOMMEND_CANCEL,param,ExtcomacctAdviserRecommendCancelVo.class);
    }

    @Override
    public ExtcombineCombineRecommendVo func_extcombine_combine_recommend(ExtcombineCombineRecommendForm param) {
        return ledT2RemoteService.callT2(EXTCOMBINE_COMBINE_RECOMMEND,param,ExtcombineCombineRecommendVo.class);
    }

    @Override
    public ExtcombineCombineRecommendCancelVo func_extcombine_combine_recommend_cancel(ExtcombineCombineRecommendCancelForm param) {
        return ledT2RemoteService.callT2(EXTCOMBINE_COMBINE_RECOMMEND_CANCEL,param,ExtcombineCombineRecommendCancelVo.class);
    }

    @Override
    public MaxRetreatQryVo func_extcomdata_max_withdrawal_rate_qry(MaxRetreatQryForm param) {
        return ledT2RemoteService.callT2(EXTCOMDATA_MAX_WITHDRAWAL_RATE_QRY,param,MaxRetreatQryVo.class);
    }

    @Override
    public SignapplyQryBackVo func_extcomacct_signapply_qry(SignapplyQryForm param) {
//        String sendercompId=param.getSendercomp_id();
//        param.setSendercomp_id(sendercompId);
        return ledT2RemoteService.callT2(EXTCOMACCT_SIGNAPPLY_QRY,param,SignapplyQryBackVo.class);
    }

    @Override
    public SignrenewVerifyVo func_extcomacct_signrenew_verify(SignrenewVerifyForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_SIGNRENEW_VERIFY,param,SignrenewVerifyVo.class);
    }

    @Override
    public SignrenewNotverifyVo func_extcomacct_signrenew_notverify(SignrenewNotverifyForm param) {
        return ledT2RemoteService.callT2(EXTCOMACCT_SIGNRENEW_NOTVERIFY,param,SignrenewNotverifyVo.class);
    }
}
